package com.podcastellano.radiopodapp;

import java.io.IOException;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;
import android.util.Log;

public class PodService extends Service {
	private static final String TAG = PodService.class.getSimpleName();
	private static final int START_STICKY = 1;
	private static final int HELLO_ID = 1;
	MediaPlayer mediaPlayer;
	private NotificationManager mNotificationManager;
	

	
	
	@Override
	public IBinder onBind(Intent arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void onCreate() {
		//Analizamos el xml y guardamos la info en playingNow
		RssParser prsPlaying= new RssParser("http://radio.podcastellano.org/now_playing.xml");
        Listening playingNow=(Listening) prsPlaying.parse();
        
        
        String ns = Context.NOTIFICATION_SERVICE;// Obtenemos el servicio de notificacion
        mNotificationManager = (NotificationManager) getSystemService(ns);
        int icon = R.drawable.noti;// Asociamos un icono y un texto apropiado
    	CharSequence tickerText = "Radio Podcastellano";
    	long when = System.currentTimeMillis();// Obtenemos la hora
		
		Context context = getApplicationContext(); //Obtenemos el contexto
		//Mostramos la informacion del podcast actual
		CharSequence contentTitle = playingNow.getTitulo();
		CharSequence contentText = playingNow.getAlbum();
		Intent notificationIntent = new Intent(this, Reproductor.class);
		PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
		//ponemos el icono de notificacion en su sitio
		Notification notification = new Notification(icon, tickerText, when);
		notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
		mNotificationManager.notify(HELLO_ID, notification);
		
		//Conectamos con el server!!
		mediaPlayer = new MediaPlayer();
		try {
			mediaPlayer.setDataSource("http://62.212.74.140:8206");
			mediaPlayer.prepareAsync();
			mediaPlayer.start();
			} catch (IOException e) {
			Log.v("AUDIOHTTPPLAYER",e.getMessage());
			}
		Log.d(TAG, "En onCreate");
		super.onCreate();
	}
	
	
	public int onStartCommand(Intent intent, int flags, int startId) {
		
		if (!mediaPlayer.isPlaying()) {
			mediaPlayer.start();
		}
		Log.v("PLAYERSERVICE","onStartCommand");
		return START_STICKY;
	}
	
	@Override
	public void onStart(Intent intent, int startId) {
		Log.d(TAG, "En onStart");
		super.onStart(intent, startId);
	}
	
	@Override
	public void onDestroy() { //Cuando matemos el servicio regemos la casa antes
		if (mediaPlayer.isPlaying())
		{
			mediaPlayer.stop();
		}
		mediaPlayer.release();
		mNotificationManager.cancel(HELLO_ID);
		Log.d(TAG, "En onDestroy");
		super.onDestroy();
	}

	

}
